# This file is part of Refix.
# 
# Refix is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# Refix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
# 
# Copyright (C) 2010-11 David Musgrove and others.

require './albacore/albacore'
require './albacore/albacore/support/albacore_helper'
require 'ftools'

CONFIG = "Release"
fullVersion = ""

task :default => [:clean, :refixtools, :version, :msbuild, :nunit, :zipTools, :zipRepo, :unversion, :refixclean, :toolsclean]
task :precommit => [:clean, :refixtools, :version, :msbuild, :nunit, :zipTools, :zipRepo, :unversion, :refixclean, :toolsclean, :refreshbinaries]

desc "Removes old zip files"
task :clean do
  Dir["*.zip"].each{|f| File.delete(f)}
end 

desc "Copies new binaries to Refix folder"
task :refreshbinaries do
  File.copy "Refix.Console/bin/Release/rfx.exe", "Refix/rfx.exe", false
  File.copy "Refix.Console/bin/Release/rfx.pdb", "Refix/rfx.pdb", false
  File.copy "Refix.Console/bin/Release/rfx.exe.config", "Refix/rfx.exe.config", false
  File.copy "Refix.Console/bin/Release/Refix.Core.dll", "Refix/Refix.Core.dll", false
  File.copy "Refix.Console/bin/Release/Refix.Core.pdb", "Refix/Refix.Core.pdb", false
  File.copy "Refix.Console/bin/Release/ICSharpCode.SharpZipLib.dll", "Refix/ICSharpCode.SharpZipLib.dll", false
  File.copy "Refix.MSBuild/bin/Release/Refix.MSBuild.dll", "Refix/Refix.MSBuild.dll", false
  File.copy "Refix.MSBuild/bin/Release/Refix.MSBuild.pdb", "Refix/Refix.MSBuild.pdb", false
  File.copy "Refix.MSBuild/bin/Release/RefixMSBuild.targets", "Refix/RefixMSBuild.targets", false
  File.copy "Refix.RepositoryBrowser/bin/Release/Refix.RepositoryBrowser.exe", "Refix/Refix.RepositoryBrowser.exe", false
  File.copy "Refix.RepositoryBrowser/bin/Release/Refix.RepositoryBrowser.pdb", "Refix/Refix.RepositoryBrowser.pdb", false
end

desc "Injects the tip revision number into the assembly version"
task :version do
    ver = `hg tip --template {rev}`
    fileIn = File.new("GlobalAssemblyInfo.cs", "r")
    fileOut = File.new("_GlobalAssemblyInfo.cs", "w")
    fileIn.each_line {
		  |line|
		  fileOut.puts line.sub(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, '\1.\2.\3.' "#{ver}")
  		if match = /((\d+)\.(\d+)\.(\d+))\.(\d+)/.match(line)
        fullVersion = match[1] + ".#{ver}"
		  end
	  }  
    fileIn.close
    fileOut.close
    File.delete("GlobalAssemblyInfo.cs")
    File.rename("_GlobalAssemblyInfo.cs", "GlobalAssemblyInfo.cs")
end

desc "Resets the assembly version"
task :unversion do
    fileIn = File.new("GlobalAssemblyInfo.cs", "r")
    fileOut = File.new("_GlobalAssemblyInfo.cs", "w")
    fileIn.each_line { |line| fileOut.puts line.sub(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, '\1.\2.\3.0') }
    fileIn.close
    fileOut.close
    File.delete("GlobalAssemblyInfo.cs")
    File.rename("_GlobalAssemblyInfo.cs", "GlobalAssemblyInfo.cs")
end

desc "Uses Refix to get build tools"
refix :refixtools do |rfx|
  rfx.command = "get"
  rfx.path_to_command = "Refix/rfx.exe"
  rfx.verbosity = "verbose"
  rfx.arguments "tools nunit-console-x86.exe 2.5.9.10348 nunit-agent-x86.exe 2.5.9.10348"
end

desc "Runs Refix clean over the solution"
refix :refixclean do |rfx|
  rfx.command = "clean"
  rfx.path_to_command = "Refix/rfx.exe"
  rfx.verbosity = "verbose"
  rfx.options "-a"
  rfx.arguments "refix.sln"
end

desc "Build the Refix solution"
msbuild do |msb|
    msb.properties :configuration => "#{CONFIG}"
    msb.targets :Clean, :Rebuild
    msb.verbosity = "Minimal"
    msb.solution = "refix.sln"
end

desc "Run the Refix unit tests"
nunit do |nunit|
    tests = Dir.glob("**/bin/#{CONFIG}/*.Tests.dll")
    TEAMCITY_NUNIT_RUNNER = ENV["teamcity.dotnet.nunitlauncher"] || ENV["teamcity_dotnet_nunitlauncher"]
    if (TEAMCITY_NUNIT_RUNNER == nil)
      nunit.path_to_command = "tools/nunit-console-x86.exe"
      nunit.options '/xml:nunit-test-report.xml /noshadow /framework:net-4.0'
    else
      nunit.path_to_command = "#{TEAMCITY_NUNIT_RUNNER}"
      nunit.options "v4.0 x86 NUnit-2.5.9 /noshadow"
    end
    nunit.assemblies = tests
end

desc "Package the Refix tools output"
zip :zipTools do |zip|
    zip.directories_to_zip "Refix.Console/bin/#{CONFIG}"
    zip.additional_files "COPYING", "COPYING.LESSER", "Refix.MSBuild/bin/#{CONFIG}/Refix.MSBuild.dll", "Refix.MSBuild/bin/#{CONFIG}/Refix.MSBuild.pdb", "Refix/RefixMSBuild.targets", "Refix.RepositoryBrowser/bin/#{CONFIG}/Refix.RepositoryBrowser.exe", "Refix.RepositoryBrowser/bin/#{CONFIG}/Refix.RepositoryBrowser.exe.config", "Refix.RepositoryBrowser/bin/#{CONFIG}/Refix.RepositoryBrowser.pdb"
    zip.output_file = "Refix.Tools-#{fullVersion}.zip"
    zip.output_path = File.dirname(__FILE__)
end
  
desc "Package the Refix repository output"
zip :zipRepo do |zip|
    zip.directories_to_zip "Refix.Repository"
    zip.additional_files "COPYING", "COPYING.LESSER"
    zip.output_file = "Refix.Repository-#{fullVersion}.zip"
    zip.output_path = File.dirname(__FILE__)
end

desc "Removes old tools"
task :toolsclean do
  Dir["tools/nunit*.*"].each{|f| File.delete(f)}
end 

